package com.oromwin.repos.server.request;

import java.io.DataInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;


import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.helper.DBHelper;
import com.oromwin.repos.data.AbstractReposTable;
import com.oromwin.repos.data.VolumeTable;
import com.oromwin.repos.server.LockObject;
import com.oromwin.repos.server.response.ListRepositoryResponse;
import com.oromwin.repos.server.response.Response;

public class ListRepositoryRequest extends AbstractRequest {
    private Log log = Logger.getLog(ListRepositoryRequest.class);

    @Override
    public void receive(DataInputStream dis, LockObject lockObject) throws ReposException {
    }

    @Override
    public Response run() throws ReposException {
        ListRepositoryResponse res = null;
        List<AbstractReposTable> reposTables = null;
        try {
            String whereClause = "VolumeStatus = 0";
            reposTables = DBHelper.selectTable(VolumeTable.class, whereClause);
            res = new ListRepositoryResponse(true, "Succeeded to list repository", reposTables);
        } catch (ReposException e) {
            if (reposTables == null) {
                reposTables = new ArrayList<AbstractReposTable>();
            }
            res = new ListRepositoryResponse(false, e.getMessage(), reposTables);
        } catch (Exception e) {
            String message = String.format("Unexpected exception, {%s}", e.getMessage());
            throw ReposException.create(log, e, message);
        }
        return res;
    }
}
